/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.web.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tianyun.cloud.exception.ExceptionContextAware;
import org.tianyun.cloud.ext.i18n.I18nMessageSource;
import org.tianyun.cloud.web.configuration.UndertowWebSocketServerFactoryCustomizer;
import org.tianyun.cloud.web.configuration.WebConfigProperties;
import org.tianyun.cloud.web.controller.HttpRequestAspect;
import org.tianyun.cloud.web.filters.ExceptionFilter;
import org.tianyun.cloud.web.filters.ServletContextFilter;
import org.tianyun.cloud.web.logger.LogPrinter;

/**
 * Web服务自动配置
 *
 * @auther ebert_chan
 */
@Configuration
public class CloudWebAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "web", ignoreInvalidFields = true)
    public WebConfigProperties webConfigProperties() {
        return new WebConfigProperties();
    }

    @Bean
    public UndertowWebSocketServerFactoryCustomizer undertowWebSocketServerFactoryCustomizer() {
        return new UndertowWebSocketServerFactoryCustomizer();
    }

    @Bean
    public LogPrinter logPrinter() {
        return new LogPrinter();
    }

    @Bean
    public ServletContextFilter servletContextFilter(WebConfigProperties webConfigProperties, I18nMessageSource i18nMessageSource) {
        return new ServletContextFilter(webConfigProperties, i18nMessageSource);
    }

    @Bean
    public ExceptionFilter exceptionFilter(ExceptionContextAware exceptionContextAware, LogPrinter logPrinter) {
        ExceptionFilter filter = new ExceptionFilter(exceptionContextAware);
        filter.setLogPrinter(logPrinter);
        return filter;
    }

    @Bean
    public HttpRequestAspect httpRequestAspect(LogPrinter logPrinter) {
        return new HttpRequestAspect(logPrinter);
    }

}
